package com.example.myapplication.util;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.xml.transform.sax.TemplatesHandler;

public class ThreadManager {
    private static volatile ThreadManager threadManager;
    private final ThreadPoolExecutor threadPoolExecutor;

    private ThreadManager() {
            //初始化线程池
            /**
             * int corePoolSize,核心线程的数量,一旦创建不会销毁
             * int maximumPoolSize, 最大线程数量
             * long keepAliveTime, 非核心线程存活时间
             * TimeUnit unit, 非核心线程存活时间的单位
             * BlockingQueue<Runnable> workQueue, 排队策略
             * ThreadFactory threadFactory,线程工厂
             */
        threadPoolExecutor = new ThreadPoolExecutor(3,10, 1, TimeUnit.MINUTES,
                new LinkedBlockingDeque<Runnable>(), Executors.defaultThreadFactory());
    }
    public static ThreadManager getThreadManager(){
        if (threadManager==null){
            synchronized (TemplatesHandler.class){
                if (threadManager==null){
                    threadManager=new ThreadManager();
                }
            }
        }
        return threadManager;
    }
    //执行任务
    public void execute(Runnable runnable){
        threadPoolExecutor.execute(runnable);
    }

    public void remove(Runnable runnable){
        threadPoolExecutor.remove(runnable);
    }
}
